package homework6.convert;

import homework6.bean.MyColor;
import org.apache.struts2.util.StrutsTypeConverter;

import java.util.Map;

/**
 * @author xshuai
 */
public class MyColorConvert extends StrutsTypeConverter {
    @Override
    public Object convertFromString(Map map, String[] strings, Class aClass) {
        MyColor color = new MyColor();
        String colorStr = strings[0];
        String[] rgb = colorStr.split(",");
        color.setRed(Integer.parseInt(rgb[0]));
        color.setGreen(Integer.parseInt(rgb[1]));
        color.setBlue(Integer.parseInt(rgb[2]));
        return color;
    }

    @Override
    public String convertToString(Map map, Object o) {
        MyColor color = (MyColor) o;
        return "rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ")";
    }
}
